package com.zklh.leetcode.lcp;

import java.util.*;

/**
 * 可以攻击国王的皇后
 *
 * @version 1.0
 * @Author tongnan
 * @Date 2023/9/15
 */
public class Solution1222 {
    public List<List<Integer>> queensAttacktheKing(int[][] queens, int[] king) {
        Map<Integer, int[]> candidates = new HashMap<>();
        int kx = king[0];
        int ky = king[1];
        for (int[] queen : queens) {
            int qx = queen[0];
            int qy = queen[1];
            if (qx == kx || qy == ky || Math.abs(qx-kx) == Math.abs(qy-ky)) {
                int directSign = sign(qx - kx) * 10 + sign(qy - ky);
                int distance = Math.abs(qx - kx) + Math.abs(qy - ky);
                if (!candidates.containsKey(directSign) || distance < candidates.get(directSign)[2]) {
                    candidates.put(directSign, new int[]{qx, qy, distance});
                }
            }
        }
        List<List<Integer>> result = new ArrayList<>();
        for (int[] candidate : candidates.values()) {
            result.add(Arrays.asList(candidate[0],candidate[1]));
        }
        return result;
    }

    private int sign(int v) {
        return Integer.compare(v, 0);
    }
}